package chat.chit.tit.mit;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import chat.chit.tit.mit.variables.Variables;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {

	private EditText etUsername;
	private EditText etPassword;
	public static final String HOST = "192.168.100.27";
	public static final int PORT = 5222;
	private String username;
	private String password;
	private XMPPConnection connection;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		postInit();
	}

	public void loginAction(View v) {
		username = etUsername.getText().toString();
		password = etPassword.getText().toString();
		if(!isEmpty(username) && !isEmpty(password)){
			connect();
		}else{
			//Show error
		}
		
	}
	public void registerAction(View v) {
		Intent in = new Intent(LoginActivity.this, RegisterActivity.class);
		startActivity(in);
	}
	
	public void postInit() {
		etUsername = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);
	}

	public void connect() {

		final ProgressDialog dialog = ProgressDialog.show(this,
				"Connecting...", "Please wait...", false);

		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				// Create a connection
				ConnectionConfiguration connConfig = new ConnectionConfiguration(
						HOST, PORT);
				connConfig.setSASLAuthenticationEnabled(false);
				connConfig.setSecurityMode(SecurityMode.disabled);
				connection = new XMPPConnection(connConfig);

				try {
					connection.connect();
					Log.i("XMPPChatDemoActivity",
							"Connected to " + connection.getHost());
				} catch (XMPPException ex) {
					Log.e("XMPPChatDemoActivity", "Failed to connect to "
							+ connection.getHost());
					Log.e("XMPPChatDemoActivity", ex.toString());
					// setConnection(null);
				}
				try {
					// SASLAuthentication.supportSASLMechanism("PLAIN", 0);
					connection.login(username, password);
					Variables.connection = connection;
					Intent in = new Intent(LoginActivity.this, MainActivity.class);
					startActivity(in);
					Log.i("XMPPChatDemoActivity",
							"Logged in as " + connection.getUser());
					// Set the status to available
					Presence presence = new Presence(Presence.Type.available);
					connection.sendPacket(presence);
				} catch (XMPPException ex) {
					Log.e("XMPPChatDemoActivity", "Failed to log in as "
							+ username);
					Log.e("XMPPChatDemoActivity", ex.toString());
					Toast.makeText(LoginActivity.this,
							"Can't get connection", Toast.LENGTH_LONG).show();
				}

				dialog.dismiss();
			}
		});
		t.start();
		dialog.show();
	}
	public static boolean isEmpty(String str){
		if(str == null || str.equals("")){
			return true;
		}else{
			return false;
		}
	}

}
